package com.gdth.http.func;

import java.util.Date;

import rx.functions.Func1;

import com.alibaba.fastjson.JSON;
import com.gdth.bank.model.TAppTokenModel;
import com.gdth.coder.encrypt.Encryption;
import com.gdth.database.bean.TAppToken;
import com.gdth.exception.HttpException;
import com.gdth.http.entity.HttpResult;
import com.gdth.http.entity.HttpTokenResult;

/** 
 * @author  Arvin
 * @version 2016-5-28 9:09:08 
 * 
 */
public class HttpTokenResultFunc<T> implements Func1<HttpTokenResult, HttpTokenResult>{
	
    @Override
    public HttpTokenResult call(HttpTokenResult t) {
       
        String content = t.getContent();
        if(null != content) {
        	String content1 = "";
        	try {
				 content1 = Encryption.decrypt(content);
//				 content1 = GZIPUtils.uncompress(content1);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				
			} 
        	
        	HttpResult param = JSON.parseObject(content1, HttpResult.class);
        	 if (param == null) {
                 throw new HttpException(102);
             }
        	if (param.getState() == 200) {
    	        TAppToken token = new TAppToken();
    	    	token.setSPublicKey(param.getPublicKey());
    	    	token.setToken(param.getToken());
    	    	token.setCjsj(new Date());

    	        //生成key
//    	        TAppTokenModel.newClientKey();
    	    	TAppTokenModel.savePublicKey(param.getPublicKey(), param.getToken());
    	    } else{
    	    	HttpException mHttpException = new HttpException(param.getState() + param.getMessage());
    	    	throw mHttpException;
    	    }
        }else {
        	 throw new HttpException(102);
        }
        

        return t;
    }
    
}
